package com.yue.modbus.job;

import com.yue.modbus.service.ReadService;
import com.yue.modbus.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class job {

    @Value("${robot.url}")
    private String robotUrl;
    @Autowired
    @Qualifier("harmonyReadServiceImpl")
    private ReadService readService;
    private HttpClient httpClient;

    @Scheduled(initialDelay = 10000, fixedDelay = 30000)
    public void notifyRobot() {
        if (readService.getAlarmSet().size() > 0) {
            // 查看机器人是否在充电桩
            if (httpClient == null) {
                httpClient = new HttpClient();
            }
            String points = StringUtils.join(readService.getAlarmSet(), ',');
            // 清除报警集合
            readService.getAlarmSet().clear();
            log.info("notify robot points:"+points);
            String res = httpClient.post(robotUrl + "/api/robotBase/run/gotowork", Map.of("type", "0","target",points), null, 30);
            log.info("robot return res:"+res);
        }
    }
}
